package com.ktjy.controller;
import com.ktjy.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author 黄军艳
 * @version 1.0
 * @date 2025/1/7  11:03
 */
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","hello thymeleaf");
        return "hello";
    }

    @GetMapping("/expression")
    public String expression(Model model){
        //添加数据到request
        //request.setAttribute("username","李四");
        //添加数据到session
        //session.setAttribute("username","王五");
        model.addAttribute("date",new Date());
        return "expression";
    }

    @GetMapping("/link")
    public String link(Model model){
        return "link";
    }

    @GetMapping("/save")
    @ResponseBody
    public String save(@RequestParam("name") String name,
                       @RequestParam("age") Integer age){
        return "name:"+name+",age:"+age;
    }

    @GetMapping("/ifunless")
    public String ifunless(Model model){
        model.addAttribute("age",16);
        return "ifunless";
    }

    @GetMapping("/switchs")
    public String switchs(Model model){
        model.addAttribute("sex","男");
        return "switchs";
    }

    @GetMapping("/each")
    public String each(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","女",19));
        list.add(new User(3,"王五","男",20));
        list.add(new User(4,"赵杰","男",21));
        model.addAttribute("list",list);
        return "each";
    }

    @GetMapping("/inline")
    public String inline(Model model){
        model.addAttribute("name","张三");
        return "inline";
    }
}
